package org.ting.book.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.ting.book.component.ResultFail;
import org.ting.book.component.ResultGenerator;
import org.ting.book.component.ResultSuccess;
import org.ting.book.constants.Constants;
import org.ting.book.model.Result;
import org.ting.book.model.UserInfo;
import org.ting.book.service.UserService;
import org.ting.book.utils.JwtUtils;

import java.net.http.HttpRequest;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    ResultGenerator success = new ResultSuccess();
    ResultGenerator fail = new ResultFail();
    @RequestMapping("/login")
    public Result<?> login(String userName, String password) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return fail.getResult("用户名或密码为空");
        }
        return userService.login(userName, password);
    }

    @RequestMapping("/getLoginUserInfo")
    public UserInfo getLoginUserInfo(HttpServletRequest request) {
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        return userService.getUserById(JwtUtils.getIdInToken(token));
    }
    @RequestMapping("/signUp")
    public Result<?> signUp(String userName, String password1, String password2) {
        //校验密码格式
        if(password1.length() < 8 || password1.length() > 16) {
            return fail.getResult("密码长度只能为8 - 16位");
        }
        for(int i = 0; i < password1.length(); i++) {
            char ch = password1.charAt(i);
            if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
                continue;
            }
            return fail.getResult("密码只能包含数字和字母");
        }

        if(!password1.equals(password2)) {
            return fail.getResult("两次输入密码不相同");
        }

        return userService.signUp(userName, password1);
    }
    @RequestMapping("/rest")
    public Result<?> restPassword(String userName, String password1, String password2) {
        if(password1.length() < 8 || password1.length() > 16) {
            return fail.getResult("密码长度只能为8 - 16位");
        }
        for(int i = 0; i < password1.length(); i++) {
            char ch = password1.charAt(i);
            if(ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') {
                continue;
            }
            return fail.getResult("密码只能包含数字和字母");
        }
        if(!password1.equals(password2)) {
            return fail.getResult("两次输入密码不相同");
        }
        return userService.restPassword(userName, password1);
    }

    @RequestMapping("/adminSignUp")
    public Result<?> adminSignUp(HttpServletRequest request, String code) {
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        return userService.adminSignUp(JwtUtils.getIdInToken(token), code);
    }
}
